package com.hulman.oms.web;

import com.hulman.oms.bean.Result;
import com.hulman.oms.bean.WeekReport;
import com.hulman.oms.service.WeekReportService;
import com.hulman.oms.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

/**
 * @Author: maxwellens
 */
@RestController
public class WeekReportController
{
    @Autowired
    private WeekReportService weekReportService;

    @GetMapping("/week-reports/{id}")
    public Result findWeekReportById(@PathVariable Integer id)
    {
        WeekReport weekReport = weekReportService.findWeekReportById(id);
        return new Result(weekReport);
    }

    @GetMapping("/week-reports")
    public Result findWeekReports(Integer page, Integer limit, Integer year, Integer week, String dateRange, Integer state, String createByName)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", limit);
        map.put("year", year);
        map.put("week", week);
        if (!StringUtil.isEmpty(dateRange))
        {
            String[] dates = dateRange.split(" - ");
            map.put("startDate", dates[0]);
            map.put("endDate", dates[1]);
        }
        map.put("state", state);
        map.put("createByName", createByName);
        return weekReportService.findWeekReportsResult(map);
    }

    @DeleteMapping("/week-reports/{id}")
    public Result deleteWeekReportById(@PathVariable Integer id)
    {
        weekReportService.deleteWeekReportById(id);
        return Result.SUCCESS;
    }

    @PostMapping("/week-reports/create")
    public Result create(WeekReport weekReport) throws IOException
    {
        weekReportService.create(weekReport);
        return Result.SUCCESS;
    }

    @PostMapping("/week-reports/submit")
    public Result submit(Integer id) throws IOException
    {
        weekReportService.submit(id);
        return Result.SUCCESS;
    }

}
